package alo.p3.work;

import alo.p3.ListNode;

public class InsertInSortedList {
    //在有序链表中插入一个结点
    ListNode InsertInSortedList(ListNode headNode, ListNode newNode){
        ListNode currentNode=headNode,previousNode = null;
        if (headNode==null)
            return newNode;
        while (currentNode!=null&&currentNode.getData()< newNode.getData()){
            previousNode=currentNode;
            currentNode=currentNode.getNext();
        }
        newNode.setNext(currentNode);
        previousNode.setNext(newNode);
        return headNode;
    }
}
